<template>
  <div>
    <el-form ref="form" v-model="form" label-width="80px" inline>
      <el-form-item label="用户昵称">
        <el-input v-model="form.searchUserName" style="width: 170px"></el-input>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          type="date"
          placeholder="起始日期"
          v-model="form.startDate"
          value-format="yyyy-MM-dd"
          style="width: 150px"
        ></el-date-picker>
        <span style="margin: 0 5px">-</span>
        <el-date-picker
          placeholder="截止日期"
          v-model="form.endDate"
          value-format="yyyy-MM-dd"
          style="width: 150px"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.prevent="searchInfo()">搜索</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="pageInfo.list" border style="width: 100%">
      <el-table-column label="序号" width="50">
        <template slot-scope="scope">
          {{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}
          <!-- 从 1 开始计数 -->
        </template>
      </el-table-column>
      <el-table-column
        prop="loginName"
        label="用户名"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="userName"
        label="昵称"
        width="100"
      ></el-table-column>
      <el-table-column prop="sex" label="性别" width="50">
        <template slot-scope="scope">
          {{ scope.row.sex === 1 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
      <el-table-column
        prop="mobile"
        label="手机号"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="createDate"
        label="创建时间"
        width="160"
      ></el-table-column>
      <el-table-column prop="type" label="用户类型" width="80">
        <template slot-scope="scope">
          {{ scope.row.type === 1 ? "管理员" : "普通用户" }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              type="primary"
              size="small"
              @click="
                $router.push({
                  name: 'userEdit',
                  query: { id: scope.row.id },
                })
              "
              style="margin-right: 10px"
            >
              编辑
            </el-button>
            <el-button
              v-if="scope.row.type == 0"
              type="primary"
              size="small"
              @click="delUser(scope.row.id)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="changePageSize"
        @current-change="changePageNum"
        :current-page="form.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="form.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "userPage",
  data() {
    return {
      pageInfo: {},
      form: {
        searchUserName: "",
        startDate: "",
        endDate: "",
        pageNum: 1,
        pageSize: 5,
      },
    };
  },
  methods: {
    searchInfo() {
      this.$axios
        .get("api/user/page", {
          params: {
            searchUserName: this.form.searchUserName,
            startDate: this.form.startDate,
            endDate: this.form.endDate,
            pageNum: this.form.pageNum,
            pageSize: this.form.pageSize,
          },
        })
        .then((response) => {
          this.pageInfo = response.data.pageInfo;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    changePageNum(newPageNum) {
      this.form.pageNum = newPageNum;
      this.searchInfo();
    },
    changePageSize(newPageSize) {
      this.form.pageSize = newPageSize;
      this.searchInfo();
    },
    delUser(id) {
      if (confirm("确认删除该用户吗？")) {
        this.$axios
          .get(`api/user/del/${id}`)
          .then((response) => {
            if (response.data.delStatus) {
              alert("删除成功");
              this.searchInfo();
            }
          })
          .catch((error) => {
            this.$message.error("删除失败");
            console.error(error);
          });
      }
    },
  },
  mounted() {
    this.searchInfo();
  },
};
</script>

<style></style>
